﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Common;
public class AvatarController
{
    public AvatarHandle Handle;
    public AvatarController(AvatarHandle handle)
    {
        //transform = t;
        //g1 = GameObject.Instantiate(GameObject.Find("Cube")) as UnityEngine.GameObject;
        //g1.name = "g1";
        //g2 = GameObject.Instantiate(GameObject.Find("Cube")) as UnityEngine.GameObject;
        //g2.name = "g2";
        Handle = handle;
        Control = Handle.Root.GetComponent<CharacterController>();
    }
    public CharacterController Control;
    private bool grounded = false;
    public void Move(Vector3 speed)
    {
        grounded = (Control.Move(speed) & CollisionFlags.CollidedBelow) != 0;
        if (grounded != Handle.InputHandle.Grounded)
        {
            if (!Handle.InputHandle.Grounded && grounded)
            {
                Handle.Movement.InitiativeJump = false;
                Handle.Animation.StopJump();
            }
            Handle.InputHandle.Grounded = grounded;
        }
        return;
        //targetPosition = transform.position + speed;
        //expectPosition = transform.TransformDirection(Vector3.forward) * Radius + targetPosition;
        //expectPosition.y = transform.position.y + ExpectHeight;
        ////Debug.DrawLine(Vector3.zero, targetPosition, Color.yellow);
        ////Debug.DrawLine(Vector3.zero, expectPosition, Color.red);
        //RaycastHit hit;
        //if (Physics.Raycast(expectPosition, Vector3.down, out hit, RaycastHitHeight, layerMask) && hit.point.y - Step <= transform.position.y)
        //{
        //    if (targetPosition.y > hit.point.y)
        //    {
        //        Grounded = false;
        //        transform.position = targetPosition;
        //    }
        //    else
        //    {
        //        Grounded = true;
        //        targetPosition.y = hit.point.y;
        //        if (targetPosition.y <= transform.position.y + Step)
        //        {
        //            transform.position = targetPosition;
        //        }
        //    }
        //}
        //else
        //{
        //    Grounded = true;
        //}
    }
    //public const float ExpectHeight = 4;
    //private const float Step = 1;
    //private const float RaycastHitHeight = 100;
    //private const float Radius = 0.8f;
    //private Vector3 targetPosition = Vector3.zero;
    //private Vector3 expectPosition = Vector3.zero;
    //private Transform transform;
    //private GameObject g1;
    //private GameObject g2;
    //private int layerMask = 1 << Define.TerrainLayer;
}



